<template>
	<view class="editUser">
		<view class="userInfo p30">
			<view class="item dis">
				<view class="itemL">头像:</view>
				<view class="itemR">
					<view class="userImg">
						<view class="imgDom">
							<image :src="userInfo.avatar" mode="aspectFill"></image>
						</view>
						<view class="editImg" @click="editPhoto">编辑头像</view>
					</view>
				</view>
			</view>
			<view class="item dis">
				<view class="itemL">昵称:</view>
				<view class="itemR">{{ userInfo.nickname || '--' }}</view>
			</view>
			<view class="item dis">
				<view class="itemL">手机号:</view>
				<view class="itemR">{{ userInfo.phone || '--' }}</view>
			</view>
			<view class="item dis">
				<view class="itemL">学校:</view>
				<view class="itemR">{{ userInfo.departmentName || '--' }}</view>
			</view>
			<view class="item dis">
				<view class="itemL">认证状态:</view>
				<view class="itemR" :class="userInfo.departmentName ? 'sucA' : 'errA'">
					{{ userInfo.departmentName ? '已认证' : '未认证' }}
				</view>
			</view>
			<view class="item dis">
				<view class="itemL">会员等级:</view>
				<view class="itemR">Lv.{{ userInfo.usergroup?.level }} {{ userInfo.usergroup?.groupTitle }}</view>
			</view>
			<view class="item dis">
				<view class="itemL">社区禁言:</view>
				<view class="itemR">
					<template v-if="!userInfo.bbsBan">正常</template>
					<template v-else>
						<text class="errA">禁言中</text>
						<view>解禁时间：{{ formatDate(userInfo.bbsBan.banExpirationTime) }}</view>
					</template>
				</view>
			</view>
			<view class="item dis">
				<view class="itemL">发帖风控:</view>
				<view class="itemR" :class="userInfo.bbsUserRiskStatus ? 'errA' : ''">
					{{ userInfo.bbsUserRiskStatus || '正常' }}
				</view>
			</view>
			<view class="item dis">
				<view class="itemL">跑腿风控:</view>
				<view class="itemR" :class="userInfo.runErrandsUserRiskStatus ? 'errA' : ''">
					{{ userInfo.runErrandsUserRiskStatus || '正常' }}
				</view>
			</view>
			<view class="item dis">
				<view class="itemL">提现状态:</view>
				<view class="itemR" :class="userInfo.isWithdrawalAllowed === 1 ? '' : 'errA'">
					{{ userInfo.isWithdrawalAllowed === 1 ? '正常' : '禁止提现' }}
				</view>
			</view>
			<view class="item dis">
				<view class="itemL">首次访问时间:</view>
				<view class="itemR">{{ formatDate(userInfo.createTime) }}</view>
			</view>
			<view class="item dis">
				<view class="itemL">最后访问时间:</view>
				<view class="itemR">{{ userInfo.lastLoginDate ? formatDate(userInfo.lastLoginDate) : '--' }}</view>
			</view>
			<view class="item dis">
				<view class="itemL">历史发帖数:</view>
				<view class="itemR color">{{ userInfo.userCount?.threadNum || 0 }}</view>
			</view>
			<view class="item dis">
				<view class="itemL">历史评论数:</view>
				<view class="itemR color">{{ userInfo.userCount?.postNum || 0 }}</view>
			</view>
		</view>
		<view class="bottom" @click="openSheet">管理用户</view>
	</view>
	<!-- 管理员管控 -->
	<up-action-sheet
		:actions="sheetOptions"
		:title="sheetTit"
		:show="sheetShow"
		:round="15"
		@select="selectSheet"
		:closeOnClickOverlay="true"
		@close="closeSheet"
		cancelText="取消"
	></up-action-sheet>
	<!-- 用户禁言 -->
	<up-popup :show="banShow" :round="10" mode="bottom" @close="closeBan" @open="banShowA">
		<view class="report riskClass">
			<view class="repTit">用户禁言</view>
			<view class="repRadio p20">
				<up-radio-group v-model="banVal" placement="row">
					<up-radio v-for="(item, index) in banList" :key="index" :label="item.label" :name="item.name"></up-radio>
				</up-radio-group>
			</view>
			<view class="reportVal p20">
				<textarea v-model="banValCon" placeholder="请输入禁言备注" />
			</view>
			<view class="repRadio p20">
				<up-radio-group v-model="banValT" placement="row">
					<up-radio v-for="(item, index) in banTiem" :key="index" :label="item.label" :name="item.name"></up-radio>
				</up-radio-group>
			</view>
			<view class="reportBut dis p20">
				<view class="butR" @click="banOKFun">提交</view>
			</view>
		</view>
	</up-popup>
	<!-- 用户风险等级设置 -->
	<up-popup :show="riskShow" :round="10" mode="bottom" @close="closeRisk" @open="riskShowA">
		<view class="report riskClass">
			<view class="repTit">用户风险等级设置</view>
			<view class="repRadio p20">
				<up-radio-group v-model="riskVal" placement="column">
					<up-radio v-for="(item, index) in riskList" :key="index" :label="item.label" :name="item.name"></up-radio>
				</up-radio-group>
			</view>
			<view class="reportBut dis p20">
				<view class="butR" @click="riskOKFun">提交</view>
			</view>
		</view>
	</up-popup>
	<!-- 用户提现 -->
	<up-modal :show="showCash" title="提示" :content="cashCon" :showCancelButton="true" @cancel="cashOff" @confirm="cashOk"></up-modal>
	<!-- 修改昵称 -->
	<up-modal :show="showName" title="提示" :showCancelButton="true" @cancel="NameOff" @confirm="showNameFun">
		<view class="slot-content">
			<up-input v-model="changeName" placeholder="请输入昵称"></up-input>
		</view>
	</up-modal>
</template>

<script setup>
import { baseUrl } from '@/config/baseUrl.js';
import { memberId, userBan, userRisk, cashType, changeNickname } from '@/config/api.js';
import { ref } from 'vue';
import { onShow, onLoad, onReachBottom } from '@dcloudio/uni-app';
import { adminEditUser } from '@/config/sheetOption.js';

const member = ref(135);
onLoad((options) => {
	console.log('onShow', options);
	member.value = options.val;
	memberIdFun();
});

// 管理员管控
const sheetShow = ref(false);
const sheetTit = ref('管理员管控');
const sheetOptions = ref([]);
const sheetType = ref('');
const openSheet = () => {
	sheetOptions.value = adminEditUser;
	sheetShow.value = true;
};
const selectSheet = (index) => {
	console.log(index);
	if (index.id == '14') {
		banShow.value = true;
	}
	if (index.id == '16' || index.id == '17') {
		riskShow.value = true;
	}
	if (index.id == '18') {
		showCash.value = true;
	}
	if (index.id == '13') {
		showName.value = true;
	}
	sheetType.value = index.id;
};
const closeSheet = () => {
	sheetShow.value = false;
	sheetOptions.value = [];
};

// 修改用户昵称
const changeName = ref('');
const showName = ref(false);
const showNameFun = async () => {
	const res = await changeNickname(member.value, {
		nickname: changeName.value
	});
	if (res.code == 0) {
		showName.value = false;
		uni.showToast({
			title: res.message,
			icon: 'none'
		});
		userInfo.value.nickname = changeName.value;
		changeName.value = '';
	} else {
		uni.showToast({
			title: res.message,
			icon: 'none'
		});
	}
};
const NameOff = () => {
	showName.value = false;
};

// 用户提现
const showCash = ref(false);
const cashCon = ref('确定要变更用户的提现状态吗？');
const cashOk = async () => {
	const res = await cashType(member.value, {});
	if (res.code == 0) {
		showCash.value = false;
		uni.showToast({
			title: res.message,
			icon: 'none'
		});
	} else {
		uni.showToast({
			title: res.message,
			icon: 'none'
		});
	}
};
const cashOff = () => {
	showCash.value = false;
};

// 禁言用户
const banList = ref([
	{ label: '违法论坛规范', name: '违法论坛规范' },
	{ label: '涉嫌广告推销', name: '涉嫌广告推销' },
	{ label: '发布违法信息', name: '发布违法信息' },
	{ label: '辱骂诽谤他人', name: '辱骂诽谤他人' },
	{ label: '侵犯他人权益', name: '侵犯他人权益' },
	{ label: '破坏产品生态', name: '破坏产品生态' },
	{ label: '对学校会产生负面舆论', name: '对学校会产生负面舆论' }
]);
const banTiem = ref([
	{ label: '一天', name: '1' },
	{ label: '一周', name: '7' },
	{ label: '一个月', name: '30' },
	{ label: '一年', name: '365' },
	{ label: '永久', name: '9999' }
]);
const banShow = ref(false);
const banVal = ref(0);
const banValT = ref(0);
const banValCon = ref('');
const banShowA = () => {};
const closeBan = () => {
	banShow.value = false;
};
const banOKFun = async () => {
	const data = {
		type: 'bbs',
		banReason: banVal.value,
		banRemark: banValCon.value,
		banTime: banValT.value
	};
	const res = await userBan(member.value, data);
	if (res.code == 0) {
		banShow.value = false;
		uni.showToast({
			title: '操作成功',
			icon: 'none'
		});
	} else {
		uni.showToast({
			title: res.message,
			icon: 'none'
		});
	}
};

// 用户风险等级设置
const riskList = ref([
	{ label: '正常', name: '0' },
	{ label: '发帖受控', name: 'controlled_thread' },
	{ label: '评论受控', name: 'controlled_post' },
	{ label: '发帖、评论受控', name: 'controlled_thread_post' },
	{ label: '发帖、评论受控且不再审核', name: 'controlled_thread_post_no_review' },
	{ label: '禁止访问', name: '403' }
]);
const riskShow = ref(false);
const riskVal = ref(0);
const riskShowA = () => {};
const closeRisk = () => {
	riskShow.value = false;
};
const riskOKFun = async () => {
	const data = {
		type: sheetType.value == 16 ? 'bbs' : 'run_errands',
		riskStatus: riskVal.value
	};
	const res = await userRisk(member.value, data);
	if (res.code == 0) {
		riskShow.value = false;
		uni.showToast({
			title: '操作成功',
			icon: 'none'
		});
	} else {
		uni.showToast({
			title: res.message,
			icon: 'none'
		});
	}
};

// 日期格式化函数
const formatDate = (timestamp) => {
	if (!timestamp) return '--';
	const date = new Date(timestamp);
	const year = date.getFullYear();
	const month = String(date.getMonth() + 1).padStart(2, '0');
	const day = String(date.getDate()).padStart(2, '0');
	const hours = String(date.getHours()).padStart(2, '0');
	const minutes = String(date.getMinutes()).padStart(2, '0');
	const seconds = String(date.getSeconds()).padStart(2, '0');

	return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
};

// 编辑头像
const editPhoto = () => {
	uni.chooseImage({
		count: 1,
		sourceType: ['album'],
		success: (chooseRes) => {
			const tempFilePaths = chooseRes.tempFilePaths;
			const token = uni.getStorageSync('token');
			uni.uploadFile({
				url: baseUrl + `/wx/admin/member/update/avatar/${member.value}`,
				filePath: tempFilePaths[0],
				name: 'file',
				formData: {},
				header: {
					Authorization: 'Bearer ' + token
				},
				success: (uploadRes) => {
					const data = typeof uploadRes.data === 'string' ? JSON.parse(uploadRes.data) : uploadRes.data;
					if (data.code == 0) {
						userInfo.value.avatar = data.data.imgUrl;
					}
				},
				fail: (err) => {
					console.error(err);
				}
			});
		},
		fail: (err) => {
			console.error('选择图片失败:', err);
		}
	});
};

// 获取用户信息
const userInfo = ref({});
const memberIdFun = async () => {
	const res = await memberId(member.value, {});
	console.log(res);
	if (res.code == 0) {
		userInfo.value = res.data.member;
	} else {
		uni.showToast({
			title: res.message,
			icon: 'none'
		});
	}
};
</script>

<style scoped lang="scss">
.editUser {
	padding-top: 20rpx;
	width: 100%;
	.userInfo {
		background-color: #fff;
		border-radius: 10rpx;
		.item {
			align-items: center;
			min-height: 70rpx;
			font-size: 28rpx;
			.itemL {
				width: 230rpx;
				color: #989898;
			}
			.itemR {
				flex: 1;
				.userImg {
					position: relative;
					width: 75rpx;
					height: 75rpx;
					.imgDom {
						width: 100%;
						height: 100%;
						border-radius: 50%;
						background-color: #ccc;
						overflow: hidden;
					}
					.editImg {
						position: absolute;
						bottom: 0;
						right: -115rpx;
						font-size: 24rpx;
						color: #289adb;
					}
				}
			}
		}
	}
	.bottom {
		position: fixed;
		right: 0rpx;
		bottom: 0rpx;
		width: 100%;
		height: 90rpx;
		line-height: 90rpx;
		text-align: center;
		background-color: #00abf7;
		color: #fff;
	}
}
/* 举报 */
.report {
	width: 100%;
	.repTit {
		width: 100%;
		line-height: 80rpx;
		text-align: center;
	}
	.reportVal {
		margin: auto;
		width: 90%;
		height: 150rpx;
		border-radius: 15rpx;
		background-color: #f3f3f3;
		textarea {
			width: 100%;
			height: 100%;
		}
	}
	.reportBut {
		justify-content: flex-end;
		width: 100%;
		.butR {
			width: 110rpx;
			height: 60rpx;
			line-height: 60rpx;
			border-radius: 30rpx;
			text-align: center;
			font-size: 26rpx;
			color: #fff;
			background-color: #1b9aee;
		}
	}
}
</style>
<style>
page {
	background-color: #f6f6f6;
}
.color {
	color: #00a8f6;
}
.errA {
	color: #fa2c19;
}
.sucA {
	color: #07c160;
}
image {
	width: 100%;
	height: 100%;
}
</style>
